﻿using System;

public class SeveralGenericCalculationsOnSet
{
    public static int[] array;

    public static void Main()
    {
        Console.WriteLine("\nExample 1\n-----------\nArray: 2, -5, 20, 9\n\nMinimal: {0}\nMaximal: {1}\nSum: {2}\nAverage: {3}\nProduct: {4}\n\n",
            Minimal(2, -5, 20, 9),
            Maximal(2, -5, 20, 9),
            Sum(2, -5, 20, 9),
            Average(2.0, -5, 20, 9),
            Product(2, -5, 20, 9));

        Console.WriteLine("Example 2\n-----------\nArray: 2, -5, 0, 1.3, 20, 9\n\nMinimal: {0}\nMaximal: {1}\nSum: {2}\nAverage: {3}\nProduct: {4}\n\n",
            Minimal(2, -5, 0, 1.3, 20, 9),
            Maximal(2, -5, 0, 1.3, 20, 9),
            Sum(2, -5, 0, 1.3, 20, 9),
            Average(2, -5, 0, 1.0, 20, 9),
            Product(2, -5, 0, 1.3, 20, 9));
    }

    public static T Minimal<T>(params T[] array)
    {
        dynamic min = array[0];

        foreach (var member in array)
            if (member < min)
                min = member;

        return min;
    }

    public static T Maximal<T>(params T[] array)
    {
        dynamic max = array[0];

        foreach (var member in array)
            if (max < member)
                max = member;

        return max;
    }

    public static T Sum<T>(params T[] array)
    {
        dynamic sum = 0;

        foreach (var member in array)
            sum += member;

        return sum;
    }

    public static T Average<T>(params T[] array)
    {
        // it must contain floating-point argument

        dynamic average = 0;

        foreach (var member in array)
            average += member;

        average /= array.Length;

        return average;
    }

    public static T Product<T>(params T[] array)
    {
        dynamic product = array[0];
        product /= array[0];

        foreach (var member in array)
            product *= member;

        return product;
    }
}